<template>
  <div class="qa-container">
    <div class="header">
      <el-button type="primary" @click="showCreateModal">新建问答</el-button>
      <el-button @click="showImportModal">批量导入</el-button>
    </div>
    
    <el-table :data="qaList" border style="width: 100%">
      <el-table-column prop="question" label="问题" />
      <el-table-column prop="answer" label="答案" show-overflow-tooltip />
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="120">
        <template #default="{ row }">
          <el-button size="small" @click="editQa(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteQa(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      v-model:current-page="pagination.current"
      v-model:page-size="pagination.size"
      :total="pagination.total"
      @current-change="fetchQas"
    />
    
    <QaCreateModal v-model="showCreate" :edit-data="editData" @success="fetchQas" />
    <QaImportModal v-model="showImport" @success="fetchQas" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
// import { getQas, deleteQa } from '@/api/knowledge'
import QaCreateModal from './QaCreateModal.vue'
import QaImportModal from './QaImportModal.vue'

const qaList = ref([])
const showCreate = ref(false)
const showImport = ref(false)
const editData = ref(null)
const pagination = ref({
  current: 1,
  size: 10,
  total: 0
})

const fetchQas = async () => {
  // const res = await getQas({
  //   page: pagination.value.current,
  //   size: pagination.value.size
  // })
  // qaList.value = res.data.list
  // pagination.value.total = res.data.total
}

const showCreateModal = () => {
  editData.value = null
  showCreate.value = true
}

const showImportModal = () => {
  showImport.value = true
}

const editQa = (row) => {
  editData.value = { ...row }
  showCreate.value = true
}

const deleteQa = async (row) => {
  try {
    await ElMessageBox.confirm('确定删除该问答吗?', '提示', {
      type: 'warning'
    })
    // await deleteQa(row.id)
    ElMessage.success('删除成功')
    fetchQas()
  } catch (e) {
    console.log('取消删除')
  }
}

onMounted(() => {
  fetchQas()
})
</script>